#include "Board.h"
#include <iostream>
#include "Snake.h"
#include <string>
#include <sstream>
#include <ctime>
#include <cstdlib>
using namespace std;





string Board::Send(){
	string output = "";
for(int i = 0; i < size; i++){
        for(int j = 0; j < size; j++){
		int a = plansza[i][j];
		output = output +int2str(a);
	}
}
return output;
}

void Board::Receive(string str){
	int tmp = 0;
	string s = str;
        for(int i = 0; i < size; i++){
                for(int j = 0; j < size; j++){
			string tt = s.substr(tmp, 1);
			plansza[i][j]= strToInt(tt);
			tmp++;
		}
	}
}

int Board::strToInt(const string &str)
{
stringstream ss(str);
int n;
ss >> n;
return n;
}

string Board::int2str(int n){
 stringstream ss;
 ss << n;
 return ss.str();
 }

int Board::SetFirst(Snake sn){
	bool shown = false;

	for(int i = 0; i < this->size; i ++){
		for(int j = 0; j < this->size; j++){
			for(int k = 0; k < sn.GetSize(); k++){
			if(sn.body[k].GetY() == i && sn.body[k].GetX() == j){
				plansza[i][j] = 1;
				shown = true;
				break;
			}
		}
			if(!shown){
                                if(plansza[i][j] != 1 && plansza[i][j] != -1 && plansza[i][j] != 8 && plansza[i][j] != 2){
			plansza[i][j] = 0;
				}
			}
			shown = false;
	}
	}
	return 0;
}

int Board::CheckSecond(Snake sn){
	for(int i = 0; i < this->size; i ++){
		for(int j = 0; j < this->size; j++){
			if(sn.body[0].GetY() == i && sn.body[0].GetX() == j){
								// sprawdzanie czy pole nie jest zajete przez jablko lub innego weza

                                if(plansza[i][j] == -1){  // jablko
                                        return -1 ;// zwiekszenie ilosci punktow i wydluzenie weza
				}

			else if(plansza[i][j] == 1){// drugi gracz
				return 1;// zwrot komunikatu o bledzie
				}
			else if(plansza[i][j] == 8){ // sciana
				return 8;
			}
		}
	}
	}
	return 0;
}

int Board::CheckFirst(Snake sn){
	for(int i = 0; i < this->size; i ++){
		for(int j = 0; j < this->size; j++){
			if(sn.body[0].GetY() == i && sn.body[0].GetX() == j){
								// sprawdzanie czy pole nie jest zajete przez jablko lub innego weza

                                if(plansza[i][j] == -1){  // jablko
                                        return -1 ;// zwiekszenie ilosci punktow i wydluzenie weza
				}

			else if(plansza[i][j] == 2){// drugi gracz
				return 2;// zwrot komunikatu o bledzie
				}
			else if(plansza[i][j] == 8){ // sciana
				return 8;
			}
		}
	}
	}
	return 0;
}

int Board::SetSecond(Snake sn){
	bool shown = false;

	for(int i = 0; i < this->size; i ++){
		for(int j = 0; j < this->size; j++){
			for(int k = 0; k < sn.GetSize(); k++){
			if(sn.body[k].GetY() == i && sn.body[k].GetX() == j){
				plansza[i][j] = 2;
				shown = true;
				break;
			}
		}
			if(!shown){
                                if(plansza[i][j] != 1 && plansza[i][j] != -1 && plansza[i][j] != 8 && plansza[i][j] != 2){
			plansza[i][j] = 0;
				}
			}
			shown = false;
	}
	}
	return 0;
}

void Board::Show(){
				for(int i = 0; i < size; i++){
		for(int j = 0; j < size; j++){
			cout << plansza[i][j];
		}
		cout << endl;
	} }


Board::Board(void)
{
this->size = 50;
}

Board::~Board(void)
{
}

void Board::SetBorders(){
			for(int i = 0; i < size; i++){
			plansza[i][0] = 8;
			plansza[0][i] = 8;
			plansza[i][size-1] = 8;
			plansza[size-1][i] = 8;
		}
}

void Board::ClearBoard(){
                        for(int i = 0; i < size; i++){
                for(int j = 0; j < size; j++){
			plansza[i][j]= 0;
		}
	}
}

void Board::SetFood(){

   // srand((unsigned)time(0));
    int i = (rand()%size)+1;
    i = (rand()%size)+1;
    i = (rand()%size)+1;
    i = (rand()%size)+1;
    int j = (rand()%size)+1;
    j = (rand()%size)+1;
    j = (rand()%size)+1;
    j = (rand()%size)+1;
    j = (rand()%size)+1;
    cout << i <<"-" << j << endl;
    if(plansza[i][j]== 0){
plansza[i][j] = -1;

    }
}


void Board::RemoveSnakes(){

    for(int i = 0; i < size; i++){
for(int j = 0; j < size; j++){
    if(plansza[i][j] > 0 && plansza[i][j] < 5){
        plansza[i][j] = 0;
    }
}
}
}
